﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.ServiceLocation;
using Pap.Core.Utilities;
using Pap.Data;
using Pap.Data.Abstraction.Utils;
using Pap.Services.FinancialServices;
using Pap.Web.Utils;
using System.Collections;

namespace Pap.Web.Modules.Loan.ViewModel
{
    /// <summary>
    /// 
    /// </summary>
    public class AssociationBankDepositeViewModel : IViewModel
    {
        /// <summary>
        /// 
        /// </summary>
        IFinanceService _FinanceService;
        /// <summary>
        /// 
        /// </summary>
        IAdministrationRepository _AdministrationRepository;
        /// <summary>
        /// 
        /// </summary>
        IImageRepository _ImageRepository;
        /// <summary>
        /// Initializes a new instance of the <see cref="AssociationBankDepositeViewModel"/> class.
        /// </summary>
        /// <param name="financeService">The finance service.</param>
        /// <param name="administrationRepository">The administration repository.</param>
        /// <param name="imageRepository">The image repository.</param>
        public AssociationBankDepositeViewModel(IFinanceService financeService, IAdministrationRepository administrationRepository, IImageRepository imageRepository)
        {
            _FinanceService = financeService;
            _AdministrationRepository = administrationRepository;
            _ImageRepository = imageRepository;
        }

        /// <summary>
        /// Gets the paid receipts.
        /// </summary>
        /// <param name="projectID">The project ID.</param>
        /// <param name="committeeID">The committee ID.</param>
        /// <returns></returns>
        public IEnumerable<IReceipt> GetPaidReceipts(Guid projectID, Guid committeeID)
        {
            return _FinanceService.GetPaidReciepts(projectID);
        }


        /// <summary>
        /// Gets the association balance.
        /// </summary>
        /// <returns></returns>
        public double GetAssociationBalance(Guid projectID)
        {
            ICommitteeBankAccount committeeBankAccount = _AdministrationRepository.FindCommitteeBankAccounts(comBankAccount => comBankAccount.CommitteeID == RegistryInfo.CommitteeID && comBankAccount.ProjectID == projectID).First();
            return committeeBankAccount.Balance.GetValueOrDefault();
                //_FinanceService.GetCommitteeBalance(RegistryInfo.CommitteeID);
        }

        /// <summary>
        /// Associations the deposit.
        /// </summary>
        /// <param name="applicationNumber">The application number.</param>
        /// <param name="projectID">The project ID.</param>
        /// <param name="attachmentIDs">The attachment I ds.</param>
        /// <param name="ReceiptIDs">The receipt I ds.</param>
        /// <param name="notes">The notes.</param>
        public void AssociationDeposit(string applicationNumber, Guid projectID, IEnumerable<Guid> attachmentIDs, IEnumerable<Guid> ReceiptIDs, string notes,double totalDeposites)
        {
            using (IUnitOfWork trans = ServiceLocator.Current.GetInstance<IUnitOfWork>())
            {
                ICommitteeBankAccount committeeBankAccount = _AdministrationRepository.FindCommitteeBankAccounts(comBankAccount => comBankAccount.CommitteeID == RegistryInfo.CommitteeID && comBankAccount.ProjectID == projectID).First();
                ICommitteeBankAccountDeposite committeeDeposite = ServiceLocator.Current.GetInstance<ICommitteeBankAccountDeposite>();
                committeeDeposite.CommitteeBankAccountDepositID = Guid.NewGuid();
                committeeDeposite.CommitteeBankAccountID = committeeBankAccount.CommitteeBankAccountID;
                committeeDeposite.DepositDate = DateTime.Now;
                committeeDeposite.Notes = notes;
                committeeDeposite.OperationNumber = applicationNumber;
                _AdministrationRepository.CreateCommitteeBankAccountDeposite(committeeDeposite);

                committeeBankAccount.Balance = committeeBankAccount.Balance + totalDeposites;

                foreach (var receiptID in ReceiptIDs)
                {
                    ICommitteeBankAccountDepositReceipt comBankAccDepositReceipt = ServiceLocator.Current.GetInstance<ICommitteeBankAccountDepositReceipt>();
                    comBankAccDepositReceipt.CommitteeBankAccountDepositReceiptID = Guid.NewGuid();
                    comBankAccDepositReceipt.CommitteeBankAccountDepositID = committeeDeposite.CommitteeBankAccountDepositID;
                    comBankAccDepositReceipt.ReceiptID = receiptID;
                    _AdministrationRepository.CreateCommitteeBankAccountDepositReceipt(comBankAccDepositReceipt);
                }
                foreach (var attachmentID in attachmentIDs)
                {
                    ICommitteeBankAccountDepositAttachment comBankAccDepositAttach = ServiceLocator.Current.GetInstance<ICommitteeBankAccountDepositAttachment>();
                    comBankAccDepositAttach.CommitteeBankAccountDepositAttachmentID = Guid.NewGuid();
                    comBankAccDepositAttach.CommitteeBankAccountDepositID = committeeDeposite.CommitteeBankAccountDepositID;
                    comBankAccDepositAttach.Attachment = attachmentID;
                    _AdministrationRepository.CreateCommitteeBankAccountDepositAttachment(comBankAccDepositAttach);
                }
                trans.Commit();
            }
        }

        /// <summary>
        /// Gets the name of the association.
        /// </summary>
        /// <returns></returns>
        public string GetAssociationName()
        {
            var assosiation = _AdministrationRepository.GetCommitteeByID(RegistryInfo.CommitteeID);
            return assosiation.CommitteeName;
        }

        /// <summary>
        /// Gets the association deposit.
        /// </summary>
        /// <param name="AssociationDepositID">The association deposit ID.</param>
        /// <returns></returns>
        public ICommitteeBankAccountDeposite GetAssociationDeposit(Guid AssociationDepositID)
        {
            return _AdministrationRepository.GetCommitteeBankAccountDepositeByID(AssociationDepositID);
        }

        /// <summary>
        /// Gets the user project ID.
        /// </summary>
        /// <returns></returns>
        public IEnumerable GetUserProjects()
        {
            return _AdministrationRepository.GetUserProjects(RegistryInfo.UserID);
        }

        //public IEnumerable<Image> GetAssociationImages(IEnumerable<Guid> AttachmentsIDs)
        //{
        //    return _ImageRepository.FindImage(image => AttachmentsIDs.Contains(image.ImageID)).Select(
        //        image => image.ImageData);
        //}
    }
}
